/*  -*- mode: c++; coding: utf-8; c-file-style: "stroustrup"; -*-

    Copyright 2008 Asier Aguirre <asier.aguirre@gmail.com>
    This file is part of Intromit.

    Intromit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Intromit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Intromit.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "render_obj_widget_label.hh"
#include "render_params.hh"
#include "render_obj_camera.hh"
#include "event_mng.hh"
#include "ogl.hh"
#include "ogl_font.hh"
#include "time_queue.hh"
#include <math.h>

// DEFINES

CLASS_INIT(render_obj_widget_label); // RENDER_OBJ_WIDGET_LABEL CLASS

render_obj_widget_label::render_obj_widget_label()
{
    CLASS_CTOR(render_obj_widget_label);
    ctor();
}

render_obj_widget_label::~render_obj_widget_label()
{
    CLASS_DTOR(render_obj_widget_label);
    dtor();
}

void render_obj_widget_label::ctor()
{
    text=NULL;
}

void render_obj_widget_label::dtor()
{
    free(text);
}

bool render_obj_widget_label::init(render_mng *ctx)
{
    return render_obj_widget::init(ctx);
}

void render_obj_widget_label::render(render_mng *ctx)
{
    render_init(ctx);
    render_base(ctx);
    if(ctx->get_pass()==RENDER_PASS_OPAQUE && root->get_infront()>0)
    {
	// render specific for widget
	render_label(ctx);
    }
    render_children(ctx);
    render_base_end(ctx);
    render_finish(ctx);
}

bool render_obj_widget_label::event(render_mng *ctx)
{
    return render_obj_widget::event(ctx);
}

unsigned render_obj_widget_label::get_type()
{
    return IS_TYPE_WIDGET_LABEL;
}

void render_obj_widget_label::render_label(render_mng *ctx)
{
    // pixel ratio test
    float c[IS_RGBA]={0.2, 0.1, 0.0, 1};
    glColor4fv(c);
    glDisable(GL_LIGHTING);

    glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT);
    glDepthMask(GL_FALSE);
    glDisable(GL_POLYGON_OFFSET_FILL);

    glBegin(GL_LINES);
    for(double x=-dims[IS_X]*0.5; x<=dims[IS_X]*0.5; x+=4.0/root->get_pixel_to_dims_ratio())
    {
	glVertex2d(x, -dims[IS_Y]*0.5);
	glVertex2d(x,  dims[IS_Y]*0.5);
    }
    glEnd();

    // blend layer
    ogl_font *f;
    if(root->get_front_blend()>=0 && (f=ctx->get_font(RENDER_FONT_FT_DEFAULT)))
    {
	float c[IS_RGBA]={1, 1, 1, root->get_front_blend()};
	glColor4fv(c);

	glPushMatrix();
	f->scale_font(0.1);
	f->write("Hi there! %.1f", root->get_depth());
	glPopMatrix();

    }
    glPopAttrib();
}
